/**
 * 
 */
package cn.ad.server.web.servlet.v2.parser;

import cn.ad.server.common.utils.EncodeUtils;
import cn.ad.server.web.servlet.v2.requests.TrackingRequest;
import cn.ad.server.web.utils.HttpUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * bes跟踪请求解释类
 * @author wangwei
 */
public abstract class BESTrackingRequestParser extends TrackingRequestParser {

	private static final String EXT_DATA = "ext_data";

	/* (non-Javadoc)
	 * @see cn.falconnect.adserver.adapter.TrackingRequestParser#readTrackingParam(javax.servlet.http.HttpServletRequest, cn.falconnect.adserver.adapter.TrackingRequest)
	 */
	@Override
	public void readTrackingParam(HttpServletRequest request, TrackingRequest trackingRequest)
			throws AdRequestParseException {
		super.readTrackingParam(request, trackingRequest);
		
		String source_base64 = request.getParameter(EXT_DATA);
		
		if (StringUtils.isEmpty(source_base64)){
			return;
		}
		
		String queryString = "";
		if (!StringUtils.isEmpty(source_base64)){
			queryString = EncodeUtils.base64Decode(source_base64);
		}
		
		Map<String, String> map = HttpUtils.getParamsMap(queryString);
		trackingRequest.setPlatform_id(NumberUtils.toInt(map.get(TrackingRequestParser.PARAM_PLATFORM_ID)));
		trackingRequest.setAid(NumberUtils.toLong(map.get(TrackingRequestParser.PARAM_ADSPACE_ID)));
		trackingRequest.setCid(NumberUtils.toLong(map.get(TrackingRequestParser.PARAM_CREATIVE_ID)));
		trackingRequest.setGuid(map.get(TrackingRequestParser.PARAM_GUID));
		trackingRequest.setSid(NumberUtils.toInt(map.get(TrackingRequestParser.PARAM_STRATEGY_ID)));
		trackingRequest.setMid(NumberUtils.toLong(map.get(TrackingRequestParser.PARAM_MATERIAL_ID)));
		trackingRequest.setVersion(map.get(TrackingRequestParser.PARAM_VERSION));
	}
}
